﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static class ExConverter
    {
        #region int

        public static int? ParseAsInt(this string input)
        {
            if (input.IsNullOrEmpty())
                return null;
            Int32 output;
            return Int32.TryParse(input, out output) ? new Int32?(output) : null;
        }

        public static int ParseInt(this string input)
        {
            return int.Parse(input);
        }

        #endregion

        #region DateTime

        public static DateTime? ParseAsDateTime(this string input)
        {
            if (input.IsNullOrEmpty())
                return null;
            DateTime output;
            return DateTime.TryParse(input, out output) ? new DateTime?(output) : null;
        }

        public static DateTime ParseDateTime(this string input)
        {
            return DateTime.Parse(input);
        }

        #endregion

        #region Decimal

        public static Decimal? ParseAsDecimal(this string input)
        {
            if (input.IsNullOrEmpty())
                return null;
            Decimal output;
            return Decimal.TryParse(input, out output) ? new Decimal?(output) : null;
        }

        public static Decimal ParseDecimal(this string input)
        {
            return Decimal.Parse(input);
        }

        #endregion

        #region Guid

        public static Guid? ParseAsGuid(this string input)
        {
            if (input.IsNullOrEmpty())
                return null;
            Guid output;
            return Guid.TryParse(input, out output) ? new Guid?(output) : null;
        }

        public static Guid ParseGuid(this string input)
        {
            return Guid.Parse(input);
        }

        public static Guid ParseDefalutGuid(this string input)
        {
            return input.ParseAsGuid() ?? Guid.Empty;
        }

        #endregion

        #region Bool

        public static bool? ParseAsBool(this string input)
        {
            if (input.IsNullOrEmpty())
                return null;
            if (input == "0")
                return false;
            if (input == "1")
                return true;
            bool output;
            return bool.TryParse(input, out output) ? new bool?(output) : null;
        }

        public static bool ParseBool(this string input)
        {
            if (input == "0")
                return false;
            if (input == "1")
                return true;
            return bool.Parse(input);
        }

        #endregion

    }
}
